<?php session_start(); 
if(!isset($_SESSION['IsLogin']))
{
    echo "<h3>To access this page, you need to login</h3>"; // Make sure they are logged in!
	
}
else
{
	echo "<h3>Hello ".$_SESSION['IsLogin']."!</h3>";
	print "<h3><a href='account.php'>Your account!</a></h3>";
}
 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>Shop</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="css/iecss.css" />
<![endif]-->
<script type="text/javascript" src="js/boxOver.js"></script>
<script type="text/javascript">  
var x = 0;
        function onTextBox_KeyUp(id)
        {
			
			x = parseInt(document.getElementById("quantity"+id).value);
			
            document.getElementById("div1").innerHTML = 'Add '+x+' items';
        }
</script>
</head>
<body>
<div id="main_content"> 
   
            <div id="menu_tab">
            <div class="left_menu_corner"></div>
                    <ul class="menu">
                         <li><a href="#" class="nav1">  Home </a></li>
                         <li class="divider"></li>
                    </ul>

             <div class="right_menu_corner"></div>
            </div><!-- end of menu tab -->
            
  	<div class="right_content">
  		<div class= "login"> <!-- form login -->
			<div class="cart_title">Login</div>
				<form action ='<?php echo $_SERVER['PHP_SELF'] ?>' method="post">
				Username: <br>
				<input type="text" name="username" value="<?php if (isset($_SESSION['IsLogin'])) echo addslashes($_SESSION['IsLogin']) ?>"><br>
				Password: <br>
				<input type="password" name="password">
				<input type= 'submit' name= 'login' value= 'Log in'>
				
		</div>
	
<?php
//Include product arrays
include 'product_data.inc';

//Include functions that are defined
include 'functions.inc';

$all_users=  arrayfile_to_array('./user_data.dat');
$username = '';
if(isset($_POST['logout']))
{
	session_destroy();
	Header("Location: index.php");
}
if(isset($_SESSION['IsLogin']))
{
	print '<div class="cart_title"></div>';
	print  "<input type= 'submit' name= 'logout' value= 'Log out'>";
	print "</form>";
	print '<div class="shopping_cart">  <!-- form Shopping cart -->';
    print '<div class="cart_title">Shopping cart</div>';
            
    print '<div class="cart_details">';
    print '<div id="div1"> items </div><br />';
			
    print '<span class="border_cart"></span>';
    print 'Total: <span class="price">0$</span>';
    print '</div>';
    print "<form action ='invoice.php' method='post'> ";    
	   
    print '<img src="cart.png" alt="" title="" width="48" height="48" border="0" /><input type = submit value="Add to cart"></div>';
        
    print '</div>';
}
//If the user clicks the log in button, I want to validate the input info.
else {
if(isset($_POST['username']))
{
    if ($actual_user_key=array_key_exists_nc($_POST['username'], $all_users)) {
        //found the user, now go get user information.
        $user_info = $all_users[$actual_user_key];
		$username = $_POST['username'];
			
        if ($user_info['password'] == ($_POST['password'])) {
			$_SESSION['pass'] = $user_info['password'];
			$_SESSION['IsLogin'] = $_POST['username'];
            echo 'Welcome ' . $_SESSION['IsLogin'] . ".";
            echo "<br>";
            // lets just make this another form with the quantities in it to pass to invoice.php
			
			Header("Location: index.php");
			
            
			
        } else {
            echo "Please check your password or ";
            ?><a href='registration.php'>register here!</a><?php

        }
    }
	}
} 
//check to see if username doesn't exist
if(array_key_exists('login',$_POST)){
    if(!array_key_exists($_POST['username'],$all_users)){
        echo "Please log in or ";
        echo "<a href='registration.php'>register here!</a>";
    }
} 

?>            
   		
		
<?php
//Authors of this code include Saori Takasaki and Jon Rivera.
        
include 'product_data.inc';

//This form will direct users to the login page.
//echo "<form action = 'login.php' method = 'post'>" ;
//This displays the first row of the table.
print '<table border="1" cellpadding="5" cellspacing="0">' ;
print '<th>Product</th> <th>Name</th> <th>Price</th> <th>Quantity</th>';

//This displays the table with a for loop.
for ($i= 0; $i < count($products); $i++)
    
	{
		 print '<tr>';
		 print "<td>{$products[$i]['Product']}</td>";
		 print "<td>{$products[$i]['Name']}</td>";
		 print "<td>{$products[$i]['Price']}</td>";
		 if(!isset($_SESSION["quantity$i"])) $_SESSION["quantity$i"] = '';
		 print "<td><input type='text' id='quantity$i' name='quantity$i' value = '".$_SESSION["quantity$i"]."' onchange='onTextBox_KeyUp($i)'/>";
		 
		 print '</tr>';
		 
	}

	print '</table>';

print "<br>";

// This creates a submit button that users use to send data.
//print "<input type= 'submit' name= 'check_out' value= 'Submit'>";

echo "</form>";

?>


